<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #toolbar>
        <a-button type="primary" @click="handleNowRefund(selectedRow)" :disabled="!hasSelectedRows || !selectedRow.canBackCard" v-auth="'ACTION_userGoodsNowBack'"> 现办现退 </a-button>
        <a-button type="primary" @click="handleUserGoodsBackCrad(selectedRow)" :disabled="!hasSelectedRows || !selectedRow.canBackCard" v-auth="'ACTION_applyUserGoodsBack'"> 退卡申请 </a-button>
        <a-button type="primary" @click="handlerDelay(selectedRow)" :disabled="!hasSelectedRows" v-auth="'ACTION_userGoodsDelay'"> 延期 </a-button>
        <a-button type="primary" @click="handlerUserGoodsServer(selectedRow)" :disabled="!hasSelectedRows" v-auth="'ACTION_userGoodsServer'"> 权益管理 </a-button>
        <a-button type="primary" @click="handlerUpgrade(selectedRow)" :disabled="!hasSelectedRows" v-auth="'ACTION_userGoodsUpgrade'"> 卡种升级 </a-button>
        <a-button type="primary" @click="handlerAddCheck(selectedRow)" :disabled="!hasSelectedRows" v-auth="'ACTION_userGoodsAddCheckRecord'"> 核验补录 </a-button>
        <a-button type="primary" @click="handleImportDataTag()" v-auth="'ACTION_userGoodsImportDataTag'">数据标识导入</a-button>
        <a-button type="primary" @click="handlerOptLLog(selectedRow)" :disabled="!hasSelectedRows" v-auth="'ACTION_userGoodsLog'"> 操作日志 </a-button>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
    </BasicTable>
    <UserGoodsDelayModal @register="registerUserGoodsDelayModal" @success="handleSuccess"></UserGoodsDelayModal>
    <UserGoodsDetailModal @register="registerUserGoodsDetailModal"></UserGoodsDetailModal>
    <UserGoodsUpgradeModal @register="registerUserGoodsUpgradeModal" @success="handleSuccess"></UserGoodsUpgradeModal>
    <UserGoodsServerDrawer @register="registerUserGoodsServerDrawer"></UserGoodsServerDrawer>
    <UserGoodsBackCradDrawer @register="registerUserGoodsBackCradDrawer" @success="handleSuccess"></UserGoodsBackCradDrawer>
    <UserGoodsAddCheckDrawer @register="registerUserGoodsAddCheckDrawer"></UserGoodsAddCheckDrawer>
    <UserOptLogModal @register="registerUserOptLogModal"></UserOptLogModal>
    <UserGoodsImportDataTagModal @register="registerUserGoodsImportDataTagModal"></UserGoodsImportDataTagModal>
    <UserGoodsEditEmployeeDrawer @register="registerUserGoodsEditEmployeeDrawer" @success="handleSuccess"></UserGoodsEditEmployeeDrawer>
    <UserGoodsEditPrivatedataDrawer @register="registerUserGoodsEditPrivatedataDrawer" @success="handleSuccess"></UserGoodsEditPrivatedataDrawer>
    <UserGoodsEditPrivateYaokedataDrawer @register="registerUserGoodsEditPrivateYaokedataDrawer" @success="handleSuccess"></UserGoodsEditPrivateYaokedataDrawer>
    <UserGoodsTagDrawer @register="registerUserGoodsTagDrawer" @success="handleSuccess"></UserGoodsTagDrawer>
    <UserGoodsSetRemarkDrawer @register="registerUserGoodsSetRemarkDrawer" @success="handleSuccess"></UserGoodsSetRemarkDrawer>
  </div>
</template>
<script lang="ts" setup name="userGoods">
import { ActionItem, BasicTable, TableAction } from '@apps/antd-admin/src/components/table';
import { useBaseTable } from '@apps/antd-admin/src/hooks';
import { useRouter } from 'vue-router';
import { Api, frozenSub, getVipcardAgreementUrl, nowRefundBackcardSub, syscAirport, userGoodsTagBlack } from './data/api';
import { useDrawer } from '@apps/antd-admin/src/components/drawer';
import { useModal } from '@apps/antd-admin/src/components/modal';
import { columns, searchFormSchema } from './data/data';
import { useMessage } from '@apps/antd-admin/src/hooks/web/useMessage';
import UserGoodsDelayModal from './components/UserGoodsDelayModal.vue';
import UserGoodsDetailModal from './components/UserGoodsDetailModal.vue';
import UserGoodsUpgradeModal from './components/UserGoodsUpgradeModal.vue';
import UserGoodsImportDataTagModal from './components/UserGoodsImportDataTagModal.vue';
import UserGoodsBackCradDrawer from './components/UserGoodsBackCradDrawer.vue';
import UserGoodsServerDrawer from './components/UserGoodsServerDrawer.vue';
import UserGoodsAddCheckDrawer from './components/UserGoodsAddCheckDrawer.vue';
import UserOptLogModal from '../../userManage/userOptLog/UserOptLogModal.vue';
import UserGoodsEditEmployeeDrawer from './components/UserGoodsEditEmployeeDrawer.vue';
import UserGoodsEditPrivatedataDrawer from './components/UserGoodsEditPrivatedataDrawer.vue';
import UserGoodsEditPrivateYaokedataDrawer from './components/UserGoodsEditPrivateYaokedataDrawer.vue';
import UserGoodsSetRemarkDrawer from './components/UserGoodsSetRemarkDrawer.vue';
import UserGoodsTagDrawer from './components/UserGoodsTagDrawer.vue';

const { createMessage, createConfirm } = useMessage();

const { prefixCls, tableContext, handleDelete, handleSuccess, handlePost, handleBatchDelete } = useBaseTable({
  designScope: 'userGoods-template',
  tableProps: {
    //表格标题
    title: '会员卡列表',
    //主键字段
    rowKey: 'userGoodsId',
    //列表查询handler
    handlerName: 'appUserGoodsListHandler',
    //列表接口
    listUrl: Api.page,
    //列定义
    columns,
    //查询表单配置
    formConfig: {
      labelWidth: 120,
      showExportButton: true,
      exportButtonOptions: {
        code: 'ACTION_userGoodsExport',
      },
      schemas: searchFormSchema,
    },
    //操作列配置
    actionColumn: {
      width: 240,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: 'right',
    },
    clickToRowSelect: true,
    rowSelection: {
      type: 'radio',
    },
  },
});

const [registerTable, { getForm, setProps, reload }, { rowSelection, hasSelectedRows, selectedRow }] = tableContext;

const [registerUserGoodsDelayModal, { openModal: openUserGoodsDelayModal }] = useModal();
const [registerUserGoodsDetailModal, { openModal: openUserGoodsDetailModal }] = useModal();
const [registerUserGoodsUpgradeModal, { openModal: openUserGoodsUpgradeModal }] = useModal();
const [registerUserOptLogModal, { openModal: openUserOptLogModal }] = useModal();
const [registerUserGoodsImportDataTagModal, { openModal: openUserGoodsImportDataTagModal }] = useModal();
const [registerUserGoodsServerDrawer, { openDrawer: openUserGoodsServerDrawer }] = useDrawer();
const [registerUserGoodsBackCradDrawer, { openDrawer: openUserGoodsBackCradDrawer }] = useDrawer();
const [registerUserGoodsAddCheckDrawer, { openDrawer: openUserGoodsAddCheckDrawer }] = useDrawer();
const [registerUserGoodsEditEmployeeDrawer, { openDrawer: openUserGoodsEditEmployeeDrawer }] = useDrawer();
const [registerUserGoodsEditPrivatedataDrawer, { openDrawer: openUserGoodsEditPrivatedataDrawer }] = useDrawer();
const [registerUserGoodsEditPrivateYaokedataDrawer, { openDrawer: openUserGoodsEditPrivateYaokedataDrawer }] = useDrawer();
const [registerUserGoodsTagDrawer, { openDrawer: openUserGoodsTagDrawer }] = useDrawer();
const [registerUserGoodsSetRemarkDrawer, { openDrawer: openUserGoodsSetRemarkDrawer }] = useDrawer();

/**
 * 行内操作栏
 */
function getTableAction(record): ActionItem[] {
  return [
    {
      label: '销售修改',
      onClick: handlerEditEmployee.bind(null, record),
      auth: 'ACTION_editUserGoodsEmployee',
    },
    {
      label: '完善资料修改',
      onClick: handlerEditPrivatedata.bind(null, record),
      auth: 'ACTION_editUserGoods',
    },
  ];
}

/**
 * 下拉操作栏
 */
function getDropDownAction(record): ActionItem[] {
  return [
    {
      label: '查看详情',
      onClick: handlerUserGoodsDetail.bind(null, record),
    },
    {
      label: '冻结',
      popConfirm: {
        title: '是否确认冻结',
        confirm: handleFreeze.bind(null, record),
      },
      ifShow: _action => {
        return record.vipCardState == 1;
      },
      auth: 'ACTION_userGoodsFreeze',
    },
    {
      label: '解冻',
      popConfirm: {
        title: '是否确认解冻',
        confirm: handleUnFreeze.bind(null, record),
      },
      ifShow: _action => {
        return record.vipCardState == 5;
      },
      auth: 'ACTION_userGoodsUnFreeze',
    },
    {
      label: '同步到机场',
      popConfirm: {
        title: '是否同步到机场',
        confirm: handleSyscAirport.bind(null, record),
      },
      ifShow: _action => {
        return record.isShowData == 0;
      },
      auth: 'ACTION_userGoodsSyscAirport',
    },
    {
      label: '会员卡标签',
      onClick: handleUserGoodsTag.bind(null, record),
      auth: 'ACTION_userGoodsTag',
    },
    {
      label: '标记黑名单',
      onClick: handleUserGoodsTagBlack.bind(null, record),
      auth: 'ACTION_userGoodsTagBlack',
    },
    {
      label: '查看协议签名',
      onClick: handleLookAgreement.bind(null, record),
      auth: 'ACTION_userGoodsLookAgreement',
    },
    {
      label: '设置备注',
      onClick: handleRemark.bind(null, record),
    },
  ];
}

function handleNowRefund(record) {
  console.info('hasSelectedRows', hasSelectedRows);
  console.info('record', record.canBackCard);
  let vipCardNo = record.vipCardNo;
  let params = {
    userGoodsId: record.userGoodsId,
    vipCardNo: vipCardNo,
    userId: record.userId,
    uesrName: record.userName,
    uesrPhone: record.mobile,
    airportId: record.airportId,
  };
  createConfirm({
    iconType: 'info',
    title: '确认是否现办现退',
    content: '确认现办现退卡号为' + vipCardNo + '的会员卡？',
    onOk: async () => {
      console.info(params);
      await nowRefundBackcardSub(params);
      handleSuccess(true, {});
    },
    onCancel: async () => {},
  });
}

function handleUserGoodsBackCrad(record) {
  openUserGoodsBackCradDrawer(true, {
    record,
    isUpdate: true,
  });
}

/**
 * 卡种延期
 * @param record
 */
function handlerDelay(record) {
  openUserGoodsDelayModal(true, {
    record,
    isUpdate: false,
  });
}

/**
 * 卡种升级
 * @param record
 */
function handlerUpgrade(record) {
  openUserGoodsUpgradeModal(true, {
    record,
    isUpdate: false,
  });
}

function handleFreeze(record) {
  let vipCardNo = record.vipCardNo;
  let userGoodsId = record.userGoodsId;
  createConfirm({
    iconType: 'info',
    title: '确认冻结',
    content: '确认冻结卡号为' + vipCardNo + '的会员卡？',
    onOk: async () => {
      await frozenSub({ userGoodsId: userGoodsId, vipCardState: 5 });
      handleSuccess(true, {});
    },
    onCancel: async () => {},
  });
}

function handleUnFreeze(record) {
  let vipCardNo = record.vipCardNo;
  let userGoodsId = record.userGoodsId;
  createConfirm({
    iconType: 'info',
    title: '确认解冻',
    content: '确认解冻卡号为' + vipCardNo + '的会员卡？',
    onOk: async () => {
      await frozenSub({ userGoodsId: userGoodsId, vipCardState: 1 });
      handleSuccess(true, {});
    },
    onCancel: async () => {},
  });
}

function handleSyscAirport(record) {
  let vipCardNo = record.vipCardNo;
  let userGoodsId = record.userGoodsId;
  createConfirm({
    iconType: 'info',
    title: '确认同步机场',
    content: '确认同步机场卡号为' + vipCardNo + '的会员卡？',
    onOk: async () => {
      await syscAirport({ userGoodsId: userGoodsId });
      handleSuccess(true, {});
    },
    onCancel: async () => {},
  });
}

function handleUserGoodsTagBlack(record) {
  let vipCardNo = record.vipCardNo;
  let userGoodsId = record.userGoodsId;
  createConfirm({
    iconType: 'info',
    title: '确认标记黑名单',
    content: '确认标记卡号为' + vipCardNo + '的会员卡为黑名单？',
    onOk: async () => {
      await userGoodsTagBlack({ userGoodsId: userGoodsId });
      handleSuccess(true, {});
    },
    onCancel: async () => {},
  });
}

function handleUserGoodsTag(record) {
  openUserGoodsTagDrawer(true, {
    record,
    isUpdate: false,
  });
}

function handlerUserGoodsDetail(record) {
  openUserGoodsDetailModal(true, {
    record,
    isUpdate: false,
  });
}

function handlerUserGoodsServer(record) {
  openUserGoodsServerDrawer(true, {
    record,
    isUpdate: false,
  });
}

function handlerAddCheck(record) {
  openUserGoodsAddCheckDrawer(true, {
    record,
    isUpdate: false,
  });
}

function handlerOptLLog(record) {
  record = { busType: 'AppUserGoods', businessId: record.userGoodsId };
  openUserOptLogModal(true, {
    record,
    isUpdate: false,
  });
}

function handleImportDataTag() {
  openUserGoodsImportDataTagModal(true, {
    isUpdate: true,
  });
}

function handlerEditEmployee(record) {
  openUserGoodsEditEmployeeDrawer(true, {
    record,
    isUpdate: true,
  });
}

function handlerEditPrivatedata(record) {
  if (record.vipcardCategoryCode == 'VIP') {
    openUserGoodsEditPrivatedataDrawer(true, {
      record,
      isUpdate: true,
    });
  } else {
    openUserGoodsEditPrivateYaokedataDrawer(true, {
      record,
      isUpdate: true,
    });
  }
}

function handleRemark(record) {
  openUserGoodsSetRemarkDrawer(true, {
    record,
    isUpdate: true,
  });
}

const router = useRouter();

async function handleLookAgreement(record) {
  let href = await getVipcardAgreementUrl({ userGoodsId: record.userGoodsId });
  window.open(href, '_blank');
}

function setSearchInfoProps(userId) {
  console.info('userId', userId);
  setProps({ searchInfo: { userId: userId } });
}

defineExpose({
  setSearchInfoProps,
});
</script>
